/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.unix;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Context;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides implementation for 'unix_pipe' primitive.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Pipe {

    /**
     * No instance of this class.
     */
    private Pipe() {
    } // end empty constructor

    /**
     * Creates a pipe.
     * @param ctxt context
     * @param unit ignored
     * @return <i>(in_descriptor, out_descriptor)</i>
     * @throws Fail.Exception if pipe creation fails
     */
    @Primitive
    public static Value unix_pipe(final CodeRunner ctxt,
                                  final Value unit)
        throws Fail.Exception, FalseExit {
        try {
            final PipedInputStream readStream = new PipedInputStream();
            final PipedOutputStream writeStream = new PipedOutputStream(readStream);
            final Channel readChannel = new Channel(readStream);
            final Channel writeChannel = new Channel(writeStream);
            final Context c = ctxt.getContext();
            final Block res = Block.createBlock(0,
                                                Value.createFromLong(c.addChannel(readChannel)),
                                                Value.createFromLong(c.addChannel(writeChannel)));
            return Value.createFromBlock(res);
        } catch (final InterruptedIOException iioe) {
            final FalseExit fe =
                FalseExit.createFromContext(ctxt.getContext());
            fe.fillInStackTrace();
            throw fe;
        } catch (final IOException ioe) {
            Unix.fail(ctxt, "pipe", ioe);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'unix_pipe(CodeRunner, Value)'

} // end class 'Pipe'
